﻿using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Base_Manage
{
    public class Base_LoginLogBusiness : BaseBusiness<Base_LoginLog>, IBase_LoginLogBusiness, ITransientDependency
    {
        public Base_LoginLogBusiness(IDbAccessor repository)
            : base(repository)
        {
        }

        #region 外部接口

        public async Task<PageResult<Base_LoginLog>> GetDataListAsync(PageInput<LoginLogInputDTO> input)
        {
            var q = GetIQueryable();
            var whereExp = LinqHelper.True<Base_LoginLog>();
            var search = input.Search;

            //筛选
            if (!search.userName.IsNullOrEmpty())
                whereExp = whereExp.And(x => x.UserName == search.userName);
            if (!search.startTime.IsNullOrEmpty())
                whereExp = whereExp.And(x => x.CreateTime >= search.startTime);
            if (!search.endTime.IsNullOrEmpty())
                whereExp = whereExp.And(x => x.CreateTime <= search.endTime);

            return await q.Where(whereExp).GetPageResultAsync(input);
        }

        public async Task<Base_LoginLog> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(Base_LoginLog data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(Base_LoginLog data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        /// <summary>
        /// 插入登录记录状态为失败
        /// </summary>
        /// <param name="_accessor"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public string SetIPLog(IHttpContextAccessor _accessor, string userName)
        {
            var ip = _accessor.HttpContext.Connection.RemoteIpAddress.ToString() + ":" + _accessor.HttpContext.Connection.RemotePort;
            // var ip = _accessor.HttpContext.Connection.LocalIpAddress.MapToIPv4().ToString() + ":" + _accessor.HttpContext.Connection.LocalPort;
            Base_LoginLog model = new Base_LoginLog();
            model.Id = IdHelper.GetId();
            model.CreateTime = DateTime.Now;
            model.LoggerIP = ip;
            model.UserName = userName;
            model.State = "登录失败";

            Insert(model);

            return model.Id;
        }

        /// <summary>
        /// 登录成功后更新登录记录状态为成功
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task UpdateIPLog(string id)
        {
            var model = await GetEntityAsync(id);
            model.State = "登录成功";
            await UpdateAsync(model);
        }

        #endregion

        #region 私有成员

        #endregion
    }
}